/*
client_verb.dm
All of the verbs required by the interface of the login system can be found here

Included:
	delete	delete the character from the database
	load	load the character (and welcome them back)
	create	create the character
*/

client/verb
	delete()
		if(busy)return
		busy=TRUE
		var
			DBI = "dbi:mysql:[sql_db]:[sql_ip]:[sql_port]"
			DBConnection/my_connection = new()
			connected = my_connection.Connect(DBI,sql_user,sql_pass)
		if(!connected)
			error("You cannot currently delete your character.")
			busy=FALSE
			return
		var/DBQuery/qry = my_connection.NewQuery("SELECT * FROM `[sql_table]` WHERE `ckey`='[mob.ckey]';")
		qry.Execute()
		if(qry.RowCount() > 0 && alert(src,"Are you sure you want to do this?","Deletion","Yes","No")=="Yes")
			var/DBQuery/qry_del = my_connection.NewQuery("DELETE FROM `[sql_table]` WHERE ckey='[ckey]';")
			qry_del.Execute()
			error("Character successfully deleted.")
			load_screen()
		else
			error("No character to delete.")
		busy=FALSE

	load()
		if(busy)return
		busy=TRUE
		Load()

		spawn(30)winshow_center(src,"main")
		winshow(src,"create",0)
		busy=FALSE
		world << format_server("Welcome [mob.name] back to the world.")

	create()
		if(busy)return
		busy=TRUE
		var/name=winget(src,"create.name","text")
		if(name)
			name=make_proper(name)
			if(length(name)>1)
				if(length(name)>20)
					error("Character name cannot be longer than 20 characters")
					busy=FALSE
					return
				var
					DBI = "dbi:mysql:[sql_db]:[sql_ip]:[sql_port]"
					DBConnection/my_connection = new()
					connected = my_connection.Connect(DBI,sql_user,sql_pass)

				if(!connected)
					error("Unable to contact database.")
					busy=FALSE
					return

				var/DBQuery/qry = my_connection.NewQuery("SELECT * FROM `[sql_db]`.`[sql_table]` WHERE name='[name]';")
				qry.Execute()

				if(qry.RowCount() > 0)
					error("Name already taken.")
					busy=FALSE
					return

				error("Character created successfully!")
				login(name)

			else error("Name too short..")
		else error("No name?")
		busy=FALSE
		world << format_server("Welcome [mob.name] to the world!")
